'use strict';

define(['services'], function (services) {
    services.common.provider('MenuService', function () {
        var menus = [];
        var menuIdMap = {}, menuUrlMap = {}, defaultMenu = null;
        var collectionMenuMap = {};
        var collectionMap = {};
        var collectionSortedMap = {};

        var reloadMenus = function ($rootScope) {
            $.ajax({
                type: 'get',
                url: '/rest/system/privilege',
                async: false,
                success: function (data) {
                    if (data != null && data.privileges != null && data.privileges.length > 0) {
                        menus = data.privileges;
                        collectionMenuMap = data.collectionPrivilegeMap;
                        collectionMap = data.collectionMap;
                        collectionSortedMap = data.collectionSortedMap;
                    } else {
                        menus = [];
                        collectionMenuMap = {};
                        collectionMap = {};
                    }
                }
            });

            menuIdMap = {}, menuUrlMap = {}, defaultMenu = null;
            for (var i = 0; i < menus.length; i++) {
                var menu = menus[i];

                if (defaultMenu == null && menu.url != null && menu.url.length > 0) {
                    defaultMenu = menu;
                }

                menuIdMap[menu.id] = menu;
                menuUrlMap[menu.url] = menu;
                if (menu.children != null && menu.children.length > 0) {
                    for (var j = 0; j < menu.children.length; j++) {
                        if (defaultMenu == null) {
                            defaultMenu = menu.children[j];
                        }

                        menuIdMap[menu.children[j].id] = menu.children[j];
                        menuUrlMap[menu.children[j].url] = menu.children[j];
                    }
                }
            }
            if (defaultMenu == null) {
                defaultMenu = {};
            }

            if ($rootScope != null) {
                $rootScope.menus = menus;
                $rootScope.menuIdMap = menuIdMap;
                $rootScope.menuUrlMap = menuUrlMap;
                $rootScope.collectionMap = collectionMap;
                $rootScope.collectionSortedMap = collectionSortedMap;
                $rootScope.defaultMenu = defaultMenu;
            }
        };

        reloadMenus();

        this.$get = function () {
            return {
                reloadMenus: reloadMenus,
                getMenus: function () {
                    return menus;
                },
                getMenuIdMap: function () {
                    return menuIdMap;
                },
                getMenuUrlMap: function () {
                    return menuUrlMap;
                },
                getDefaultMenu: function () {
                    return defaultMenu;
                },
                getDefaultMenuUrl: function () {
                    var url = defaultMenu.url;
                    var arr = url.split('/');
                    if (arr.length > 3 && arr[1] == 'collection') {
                        for (var collectionId in collectionMap) {
                            arr[2] = collectionId;
                            break;
                        }
                    }
                    return arr.join('/');
                },
                getCollectionMenuMap: function () {
                    return collectionMenuMap;
                },
                getCollectionMap: function () {
                    return collectionMap;
                },
                getCollectionSortMap: function () {
                    return collectionSortedMap;
                },
                getCollectionId: function (path) {
                    var arr = path.split('/');
                    if (arr.length > 3 && arr[1] == 'collection') {
                        return arr[2];
                    }
                    return '';
                },
                getOriginMenuUrl: function (path) {
                    var arr = path.split('/');
                    if (arr.length > 3 && arr[1] == 'collection') {
                        arr[2] = ':id';
                    }
                    return arr.join('/');
                }
        }
    };
});
})
;